# 设置基础镜像
FROM golang:1-alpine AS builder

# 设置工作目录为 /root/bark/bark-server
WORKDIR /root/bark/bark-server

# 复制当前目录中的文件到工作目录
COPY . .

# 安装依赖并编译应用
RUN set -ex \
    && apk add git gcc libc-dev \
    && BUILD_VERSION=$(git describe --tags) \
    && BUILD_DATE=$(date "+%F %T") \
    && COMMIT_SHA1=$(git rev-parse HEAD) \
    && go install -trimpath -ldflags \
            "-X 'main.version=${BUILD_VERSION}' \
             -X 'main.buildDate=${BUILD_DATE}' \
             -X 'main.commitID=${COMMIT_SHA1}' \
             -w -s"

# 设置基础镜像
FROM alpine

LABEL maintainer="ycpay <ycpay.org>"

# 设置 nsswitch.conf 配置
RUN echo 'hosts: files dns' > /etc/nsswitch.conf

# 设置时区
ARG TIMEZONE=Asia/Shanghai
ENV TZ ${TIMEZONE}

RUN set -ex \
    && apk upgrade \
    && apk add --no-cache bash ca-certificates tzdata \
    && ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone

# 从构建阶段复制编译后的应用和 entrypoint.sh
COPY --from=builder /root/bark/bark-server /usr/local/bin/bark-server
COPY --from=builder /root/bark/bark-server/deploy/entrypoint.sh /entrypoint.sh

# 设置数据卷和暴露端口
VOLUME /data
EXPOSE 8080

# 定义容器启动命令
ENTRYPOINT ["/entrypoint.sh"]
CMD ["bark-server"]
